package com.zzptc.zzsgz.bdsjws;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zzptc.zzsgz.bdsjws.com.Constants;
import com.zzptc.zzsgz.bdsjws.db.BBXDao;
import com.zzptc.zzsgz.bdsjws.db.DaoMaster;
import com.zzptc.zzsgz.bdsjws.db.DaoSession;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.BBX;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import org.greenrobot.greendao.database.Database;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

import static java.util.concurrent.TimeUnit.MILLISECONDS;


public class MyApplication extends Application {
    static DaoSession daoSession;
    static DaoSession mobiledaoSession;

    public static List<Contact> getList() {
        return list;
    }

    static List<Contact> list;

    //功能：1、提供全局的上下文 2、初始化KLOG
    static Context context;

    //真正的入口方法
    @Override
    public void onCreate() {
        super.onCreate();
        initClient();
        context = getApplicationContext();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);
        initDatabase();
        initDatabase1();
        new Thread(new Runnable() {
            @Override
            public void run() {
                initDataTable();
                //将外部的数据库要拷贝到项目中
                try {
                    MyUtil.copyFileToMobile(context,"mobile.db","mobile.db");
                    list = MyUtil.getAllContacts();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public void initClient() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .build();
        OkHttpUtils.initClient(okHttpClient);

    }

    public static Context getContext() {

        return context;
    }


    //初始化 GreenDao  bbx
    private void initDatabase() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, Constants.DATABASE_NAME);
        Database database = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        daoSession = daoMaster.newSession();
    }

    // bbx
    public static DaoSession getDaoSession() {

        return daoSession;
    }


    //初始化 GreenDao   外部数据库
    private void initDatabase1() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context , "mobile.db");
        Database database = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        mobiledaoSession = daoMaster.newSession();
    }

    // 外部数据库
    public static DaoSession getMobileDaoSession() {

        return mobiledaoSession;
    }



    //将数据新增到表  要对表进行增删改成  必须要活的BBXDao对象  只能操作一次  共享首选项
    public void initDataTable() {
        DaoSession daoSession = getDaoSession();
        BBXDao bbxDao = daoSession.getBBXDao();

//        try {
//            MyUtil.copyFileToMobile(context , "mobile.db","aaaa.db");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        SharedPreferences sharedPreferences = MyUtil.getSharedPre();
        boolean b = sharedPreferences.getBoolean("inserttable", false);
        if (!b){

        //数据源放在  string.xml中
        //怎么读取String。xml中的文件
        String[] names = getResources().getStringArray(R.array.bbx_name);
        String[] icons = getResources().getStringArray(R.array.bbx_icon);

        for (int i = 0; i < names.length; i++) {
            BBX bbx = new BBX(null, names[i], icons[i]);
            try {
                long m = bbxDao.insert(bbx);
                if (m > 0) {
                    System.out.println("新增成功");
                } else {
                    System.out.println("新增失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //新增完毕，将共享首选项的值改为true
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("inserttable" , true);
            editor.commit();
    }


    }

}
